package edu.scau.system.handle;

import cn.hutool.json.JSONUtil;
import edu.scau.core.constants.AjaxResult;
import edu.scau.core.constants.HttpStatus;
import edu.scau.core.util.ServletUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限错误403处理类，返回权限不足
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        // 设置响应状态码为 403
        int code = HttpStatus.FORBIDDEN;
        String result = JSONUtil.toJsonStr(AjaxResult.error(code,"用户权限不足"));
        ServletUtils.renderString(code,response,result);
    }
}